package com.hk.mybatis.core;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;

import java.util.Map;

public class ColumnResolver<T> {
    private Map<String, ColumnCache> columnMap = null;
    private boolean initColumnMap = false;
    private void tryInitCache(Class<?> lambdaClass) {
        if (!initColumnMap) {
            columnMap = LambdaUtils.getColumnMap(lambdaClass);
            Assert.notNull(columnMap, "can not find lambda cache for this entity [%s]", lambdaClass.getName());
            initColumnMap = true;
        }
    }
    public String resolve(String fieldName, Class<T> cls) {
        tryInitCache(cls);
        ColumnCache columnCache = columnMap.get(LambdaUtils.formatKey(fieldName));
        return columnCache == null ? null : columnCache.getColumn();
    }
}
